package com.gec.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gec.model.system.ITripItinerary;
import com.gec.model.system.ITripUser;
import com.gec.model.vo.ITripUserQueryVo;
import com.gec.model.vo.RouterVo;
import com.gec.system.mapper.ITripUserMapper;
import com.gec.system.service.ITripMenuService;
import com.gec.system.service.ITripUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author chenzhiling
 * @since 2024-01-20
 */
@Service
public class ITripUserServiceImpl extends ServiceImpl<ITripUserMapper, ITripUser> implements ITripUserService {

    @Autowired
    private ITripMenuService iTripMenuService;

    @Override
    public IPage<ITripUser> selectPage(IPage<ITripUser> page1, ITripUserQueryVo iTripUser) {
        IPage<ITripUser> iPage = this.baseMapper.selectPage(page1, iTripUser, null);
        return iPage;
    }

    @Override
    public void updateActivated(Long id, Integer status) {
        //1.查询出当前用户
        ITripUser iTripUser = this.baseMapper.selectById(id);
        //2.设置状态
        iTripUser.setActivated(status);
        //3.更新
        this.baseMapper.updateById(iTripUser);
    }

    @Override
    public ITripUser getUserInfoUserName(String username) {
        QueryWrapper<ITripUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);

        ITripUser iTripUser = this.baseMapper.selectOne(queryWrapper);

        return iTripUser;
    }

    @Override
    public Map<String, Object> getUserInfo(String username) {
        Map<String, Object> map = new HashMap<>();
        ITripUser iTripUser = this.getUserInfoUserName(username);

        //根据用户id获取菜单权限值
        List<RouterVo> routerVoList = iTripMenuService.findUserMenuList(iTripUser.getId());
        //根据用户id获取用户按钮权限
        List<String> permsList = iTripMenuService.findUserPermsList(iTripUser.getId());

        //当前权限控制使用不到，我们暂时忽略
        map.put("name", iTripUser.getName());
        map.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("roles", "[admin]");
//        String authoritiesString = (String) redisTemplate.opsForValue().get(username);
//        System.out.println(authoritiesString);
//        map.put("roles", authoritiesString);
        System.out.println("菜单权限值id列表："+routerVoList);
        System.out.println("用户按钮值id列表："+permsList);

        map.put("buttons", permsList);
        map.put("routers", routerVoList);


        return map;
    }

    @Override
    public ITripUser selectByName(String name) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username",name);//相当于where typeNo=1
        List<ITripUser> list = baseMapper.selectList(wrapper);
        return list.size()==0 ?null :list.get(0);
    }
}
